<%@page import="com.google.appengine.api.datastore.EntityNotFoundException"%>
<%@page import="nl.project.data.controller.MembersDAO" %>
<%@page import="java.util.ArrayList" %>
<%@taglib uri="http://java.sun.com/jsp/jstl/core"      prefix="c" %>
<%@taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>

<%! ArrayList<String> errors = new ArrayList<String>(); %>
<%
	//if user submitted data, check if authentication succeeds
	if (request.getParameter("code") != null && request.getParameter("code") != "" &&
			request.getParameter("password") != null && request.getParameter("password") != "") {
		boolean isAllowed = false;
		//authentication check
		try {
			isAllowed = MembersDAO.authenticate(request.getParameter("code"), request.getParameter("password"));
			if (!isAllowed) {
				errors.add("Gegevens onjuist. Kloppen de code en het wachtwoord?");
			}
		} catch (EntityNotFoundException e) {
			errors.add("Inlogcode niet gevonden. Check de code opnieuw a.u.b.");
		}
		if (errors.size() == 0) {
			application.getRequestDispatcher("/for").forward(request, response);	
		}		
	}
%>
<!DOCTYPE HTML>
<html>
	<head>
		<title>Inlogpagina</title>
	</head>
	<body>
		<h2>Inlog pagina</h2>
		<%
			for (String msg : errors) {
				out.print("<span class='error'>" + msg + "</span><br>");
			}
		%>
		<p>Vul hieronder uw toegestuurde code en wachtwoord in</p>
		<form action="" method="POST">
			Verzonden code: <input type="text" name="code"><br>
			Verzonden wachtwoord: <input type="password" name="password"><br>
			<input type="submit" value="Login">
		</form>
		<%
			// because fields stay the same for all users...
			errors.clear();
		%>
	</body>
</html>